<?php
//切换标签插件
class cy_tab extends WP_Widget {

	function cy_tab() {
		$widget_ops = array('description' => '标签切换的小工具，显示最热文章、最新文章、随机文章');
		$this->WP_Widget('cy_tab', '&#xE000; 切换标签', $widget_ops);
	}

	function form($instance) {
		$days = isset($instance['days']) ? $instance['days'] : 90;
		$posts_num = isset($instance['posts_num']) ? $instance['posts_num'] : 10;
		$order = isset($instance['orderby']) ? $instance['orderby'] : '评论次数';
		$orderby = array('评论数量','浏览次数');
?>
	<p>
		<label>文章数量：
			<input id="<?php echo $this->get_field_id('posts_num')?>" name="<?php echo $this->get_field_name('posts_num')?>" type="text" value="<?php echo $posts_num?>" size="6" />
		</label>
	</p>

	<p>
		<label>统计天数：
			<input id="<?php echo $this->get_field_id('days')?>" name="<?php echo $this->get_field_name('days')?>" type="text" value="<?php echo $days?>" size="6" />
		</label>
	</p>

	<p>
		<label>排序方式：
			<select name="<?php echo $this->get_field_name('orderby')?>">
<?php
foreach ($orderby as $order){
	echo "				<option value=\"$order\"";
	if ($instance['orderby']==$order){
		echo 'selected="selected"';
	}
	echo ">$order</option>\n";
}?>
			</select>
		</label>
	</p>
<?php
	}

	function update($new_instance, $old_instance) {
		return $new_instance;
	}

	function widget($args, $instance) {
		extract( $args );
		$posts_num = $instance['posts_num'];
		$days = $instance['days'];
		$order = $instance['orderby'];
		echo $before_widget;
		echo cy_widget_tab($posts_num,$days,$order);
		echo $after_widget;
	}
}
register_widget('cy_tab');

function cy_widget_tab($posts_num,$days,$order){
?> 
<ul class="cy_tab_menu btn">
	<li class="current">热门文章</li>
	<li>最新发布</li>
	<li>随机推荐</li>
</ul>
<div class="cy_tab_list">
	<ul class="cy_tab_post">
<?php
global $wpdb;
if ($order == "浏览次数"){
$posts = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_type = 'post' AND TO_DAYS(now()) - TO_DAYS(post_date) < $days AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $posts_num");
}
else {
$posts = $wpdb->get_results("SELECT ID , post_title , comment_count FROM $wpdb->posts WHERE post_type = 'post' AND TO_DAYS(now()) - TO_DAYS(post_date) < $days AND ($wpdb->posts.`post_status` = 'publish' OR $wpdb->posts.`post_status` = 'inherit') ORDER BY comment_count DESC LIMIT 0 , $posts_num ");
}
$i = 0; foreach($posts as $post) : $i++?>
		<li><a href="<?php echo get_permalink( $post->ID )?>"><span <?php echo $i < 4 ? 'class="hot"' : '';  ?>><?php $order == "评论数量" ? print $post->comment_count.'条评论' : print $post->views.' ℃';?></span><?php echo $post->post_title?></a></li>
<?php endforeach;?>
	</ul>
	<ul class="cy_tab_post hide">
<?php $myposts = get_posts("numberposts=$posts_num&offset=0");foreach($myposts as $post) :?>
		<li><a href="<?php echo get_permalink( $post->ID )?>"><?php echo $post->post_title?></a></li>
<?php endforeach?>
	</ul>
	<ul class="cy_tab_post hide">
<?php $myposts = get_posts("numberposts=$posts_num&orderby=rand");foreach($myposts as $post) :?>
		<li><a href="<?php echo get_permalink( $post->ID )?>"><?php echo $post->post_title?></a></li>
<?php endforeach?>
<?php wp_reset_query()?>
	</ul>
</div>
<?php
}
?>